
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5、Block · zhoufei-iosknowledges</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhoufei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="5.1.html" />
    
    
    <link rel="prev" href="../neicun/4.5.html" />
    

    
        <link rel="shortcut icon" href='../../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"zhou415770319","repo":"iosKnowledges","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.feiyum.cn/index/" target="_blank" class="custom-link">我的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    iOS面试题总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../ui/ui.html">
            
                <a href="../ui/ui.html">
            
                    
                    1、UI视图相关面试题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../ui/UITableview.html">
            
                <a href="../ui/UITableview.html">
            
                    
                    1.1UITableview相关面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../ui/shujutongbu.html">
            
                <a href="../ui/shujutongbu.html">
            
                    
                    1.2数据源同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../ui/shijianchuandi.html">
            
                <a href="../ui/shijianchuandi.html">
            
                    
                    1.3UI事件传递与响应
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../ui/tuxiangxianshi.html">
            
                <a href="../ui/tuxiangxianshi.html">
            
                    
                    1.4UI图像显示原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../ui/kadundiaozheng.html">
            
                <a href="../ui/kadundiaozheng.html">
            
                    
                    1.5UI卡顿掉帧原因
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../ui/yibuhuizhi.html">
            
                <a href="../ui/yibuhuizhi.html">
            
                    
                    1.6异步绘制原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../ui/lipingxuanran.html">
            
                <a href="../ui/lipingxuanran.html">
            
                    
                    1.7离屏渲染
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../oc/2.html">
            
                <a href="../oc/2.html">
            
                    
                    2、OC语言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../oc/2.1.html">
            
                <a href="../oc/2.1.html">
            
                    
                    2.1OC相关面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../oc/2.2.html">
            
                <a href="../oc/2.2.html">
            
                    
                    2.2分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../oc/2_4.html">
            
                <a href="../oc/2_4.html">
            
                    
                    2.4扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../oc/2.5.html">
            
                <a href="../oc/2.5.html">
            
                    
                    2.5代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../oc/2.6.html">
            
                <a href="../oc/2.6.html">
            
                    
                    2.6通知
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../oc/2.7.html">
            
                <a href="../oc/2.7.html">
            
                    
                    2.7KVO
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../oc/2.8.html">
            
                <a href="../oc/2.8.html">
            
                    
                    2.8KVC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../oc/2.9.html">
            
                <a href="../oc/2.9.html">
            
                    
                    2.9属性关键字
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../runtime/3.html">
            
                <a href="../runtime/3.html">
            
                    
                    3、Runtime
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../runtime/3.1.html">
            
                <a href="../runtime/3.1.html">
            
                    
                    3.1对象、类对象、元类对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../runtime/3.2.html">
            
                <a href="../runtime/3.2.html">
            
                    
                    3.2消息传递机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../runtime/3.3.html">
            
                <a href="../runtime/3.3.html">
            
                    
                    3.3消息转发流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../runtime/3.4.html">
            
                <a href="../runtime/3.4.html">
            
                    
                    3.4Method-Swizzling
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../neicun/4.html">
            
                <a href="../neicun/4.html">
            
                    
                    4、内存
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../neicun/4.1.html">
            
                <a href="../neicun/4.1.html">
            
                    
                    4.1引用计数表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../neicun/4.2.html">
            
                <a href="../neicun/4.2.html">
            
                    
                    4.2弱引用表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../neicun/4.3.html">
            
                <a href="../neicun/4.3.html">
            
                    
                    4.3ARC、MRC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../neicun/4.4.html">
            
                <a href="../neicun/4.4.html">
            
                    
                    4.4循环引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../neicun/4.5.html">
            
                <a href="../neicun/4.5.html">
            
                    
                    4.5内存管理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.6" data-path="5.html">
            
                <a href="5.html">
            
                    
                    5、Block
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="5.1.html">
            
                <a href="5.1.html">
            
                    
                    5.1Block本质
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="5.2.html">
            
                <a href="5.2.html">
            
                    
                    5.2截获变量特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="5.3.html">
            
                <a href="5.3.html">
            
                    
                    5.3内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="5.4.html">
            
                <a href="5.4.html">
            
                    
                    5.4循环引用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../duoxiancheng/6.html">
            
                <a href="../duoxiancheng/6.html">
            
                    
                    6、多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../duoxiancheng/6.1.html">
            
                <a href="../duoxiancheng/6.1.html">
            
                    
                    6.1GCD、nsoperation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../duoxiancheng/6.2.html">
            
                <a href="../duoxiancheng/6.2.html">
            
                    
                    6.2资源共享
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../duoxiancheng/6.3.html">
            
                <a href="../duoxiancheng/6.3.html">
            
                    
                    6.3线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../duoxiancheng/6.4.html">
            
                <a href="../duoxiancheng/6.4.html">
            
                    
                    6.4SpinLock等
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../runloop/7.md">
            
                <span>
            
                    
                    7、RunLoop
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../runloop/7.1.md">
            
                <span>
            
                    
                    7.1事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../runloop/7.2.md">
            
                <span>
            
                    
                    7.2用户态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../runloop/7.3.md">
            
                <span>
            
                    
                    7.3核心态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../runloop/7.4.md">
            
                <span>
            
                    
                    7.4常驻线程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../network/8.md">
            
                <span>
            
                    
                    8、网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../network/8.1.md">
            
                <span>
            
                    
                    8.1HTTPS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../network/8.2.md">
            
                <span>
            
                    
                    8.2对称加密算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../network/8.3.md">
            
                <span>
            
                    
                    8.3非对称加密
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../network/8.4.md">
            
                <span>
            
                    
                    8.4DNS解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../network/8.5.md">
            
                <span>
            
                    
                    8.5HTTP、TCP
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >5、Block</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#block"><b></b>Block</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="block"><a name="block" class="anchor-navigation-ex-anchor" href="#block"><i class="fa fa-link" aria-hidden="true"></i></a><a name="block" class="plugin-anchor" href="#block"><i class="fa fa-link" aria-hidden="true"></i></a>Block</h2>
<h4 id="block&#x5B9E;&#x73B0;&#x539F;&#x7406;"><a name="block&#x5B9E;&#x73B0;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#block&#x5B9E;&#x73B0;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="block&#x5B9E;&#x73B0;&#x539F;&#x7406;" class="plugin-anchor" href="#block&#x5B9E;&#x73B0;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>Block&#x5B9E;&#x73B0;&#x539F;&#x7406;</h4>
<p>&#x8981;&#x60F3;&#x77E5;&#x9053;Block&#x7684;&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#xFF0C;&#x9700;&#x8981;&#x77E5;&#x9053;Block&#x7F16;&#x8BD1;&#x5B8C;&#x540E;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#x5B50;&#xFF0C;&#x4F7F;&#x7528;clang&#x53EF;&#x770B;&#x5230;Block&#x7F16;&#x8BD1;&#x5B8C;&#x540E;&#x7684;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#x3002;</p>
<p>Block&#x7684;&#x8BED;&#x6CD5;&#x770B;&#x7740;&#x7279;&#x522B;&#xFF0C;&#x4F46;&#x5B83;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4F5C;&#x4E3A;&#x6781;&#x666E;&#x901A;&#x7684;C&#x8BED;&#x8A00;&#x6E90;&#x4EE3;&#x7801;&#x6765;&#x5904;&#x7406;&#x7684;&#x3002;&#x901A;&#x8FC7;&#x652F;&#x6301;Block&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x5C06;&#x542B;&#x6709;Block&#x8BED;&#x6CD5;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x4E3A;C&#x8BED;&#x8A00;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x4F5C;&#x4E3A;&#x6781;&#x4E3A;&#x666E;&#x901A;&#x7684;C&#x8BED;&#x8A00;&#x6E90;&#x4EE3;&#x7801;&#x88AB;&#x7F16;&#x8BD1;&#x3002;</p>
<pre><code>clang -rewrite-objc &#x6587;&#x4EF6;&#x540D;
</code></pre><p>&#x4E0A;&#x8FF0;&#x547D;&#x4EE4;&#x4F7F;&#x7528;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x9879;&#x76EE;&#x662F;&#x6CA1;&#x95EE;&#x9898;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;&#x5728;single APP&#x9879;&#x76EE;&#x4E2D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;<em>UIKit/UIKit.h&#x2019; file not found</em></p>
<p>&#x5728;single APP&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code>&#x6A21;&#x62DF;&#x5668; xcrun -sdk iphonesimulator clang -rewrite-objc xxx.m
&#x771F;&#x673A; xcrun -sdk iphoneos clang -rewrite-objc xxx.m
</code></pre><p>&#x4F7F;&#x7528;&#x547D;&#x4EE4;&#x540E;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A; <strong>xxx.cpp</strong>&#x7684;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6765;&#x5177;&#x4F53;&#x770B;&#x4E0B;Block&#x7F16;&#x8BD1;&#x540E;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5148;&#x6765;&#x770B;&#x547D;&#x4EE4;&#x884C;&#x9879;&#x76EE;&#xFF1A;</p>
<pre><code>int main(int argc, const char * argv[]) {
    @autoreleasepool {
        void (^blk)(void) = ^{
            printf(&quot;Block\n&quot;);
        };
        blk();
    }
    return 0;
}
</code></pre><p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x7B80;&#x5355;&#x7684;&#x65E0;&#x8FD4;&#x56DE;&#x503C;&#x65E0;&#x53C2;&#x6570;&#x7684;Block&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x4F7F;&#x7528;clang&#x7F16;&#x8BD1;&#x540E;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x53EA;&#x628A;&#x6709;&#x7528;&#x7684;&#x90E8;&#x5206;&#x8D34;&#x51FA;&#xFF1A;</p>
<pre><code>struct __block_impl {
  void *isa;//block isa&#x6307;&#x9488;
  int Flags;
  int Reserved;
  void *FuncPtr;//&#x51FD;&#x6570;&#x6307;&#x9488;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  //&#x521D;&#x59CB;&#x5316;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
    impl.isa = &amp;_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

            printf(&quot;Block\n&quot;);
        }

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;//block&#x5927;&#x5C0F;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        void (*blk)(void) = ((void (*)())&amp;__main_block_impl_0((void *)__main_block_func_0, &amp;__main_block_desc_0_DATA));//block&#x8868;&#x8FBE;&#x5F0F;&#x8F6C;&#x6362;
        ((void (*)(__block_impl *))((__block_impl *)blk)-&gt;FuncPtr)((__block_impl *)blk);//block&#x8C03;&#x7528;
    }
    return 0;
}
</code></pre><p>&#x5982;&#x4E0A;&#x53D8;&#x6362;&#x540E;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x901A;&#x8FC7;Block&#x4F7F;&#x7528;&#x7684;&#x533F;&#x540D;&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x88AB;&#x4F5C;&#x4E3A;&#x7B80;&#x5355;&#x7684;C&#x8BED;&#x8A00;&#x51FD;&#x6570;&#x6765;&#x5904;&#x7406;&#xFF0C;&#x53E6;&#x5916;Block&#x8BED;&#x6CD5;&#x6240;&#x5C5E;&#x7684;&#x51FD;&#x6570;&#x540D;&#xFF08;&#x6B64;&#x5904;&#x4E3A;main&#xFF0C;&#x540E;&#x8FB9;&#x4F1A;&#x8D34;&#x4E0A; &#x51FD;&#x6570;&#x540D;&#x4E3A; viewDidLoad&#xFF09;&#x548C;&#x8BE5;Block&#x8BED;&#x6CD5;&#x5728;&#x8BE5;&#x51FD;&#x6570;&#x51FA;&#x73B0;&#x7684;&#x987A;&#x5E8F;&#x503C;&#x6765;&#x7ED9;&#x7ECF;clang&#x53D8;&#x6362;&#x7684;&#x51FD;&#x6570;&#x547D;&#x540D;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x5177;&#x4F53;&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x6765;&#x770B;&#x770B;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#xFF1A;
 __main_block_impl_0&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x58F0;&#x660E;&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x662F;impl,&#x7B2C;&#x4E8C;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#x662F;Desc&#x3002;</p>
<pre><code>struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
};
</code></pre><p>__block_impl &#x7ED3;&#x6784;&#x4F53;&#x7684;&#x58F0;&#x660E;&#xFF1A;</p>
<pre><code>struct __block_impl {
  void *isa;//block isa&#x6307;&#x9488;
  int Flags;
  int Reserved;
  void *FuncPtr;//&#x51FD;&#x6570;&#x6307;&#x9488;
};
</code></pre><p>__main_block_desc_0 &#x7ED3;&#x6784;&#x4F53;&#x58F0;&#x660E;&#xFF1A;</p>
<pre><code>static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;//block&#x5927;&#x5C0F;
}
</code></pre><p>__main_block_impl_0&#x5B9E;&#x4F8B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code>__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
    impl.isa = &amp;_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
</code></pre><p>Block&#x8868;&#x8FBE;&#x5F0F;&#x751F;&#x6210;&#x90E8;&#x5206;&#xFF1A;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;__main_block_impl_0&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x5E76;&#x8D4B;&#x503C;&#x7ED9;blk&#x53D8;&#x91CF;&#x3002;</p>
<pre><code>void (*blk)(void) = ((void (*)())&amp;__main_block_impl_0((void *)__main_block_func_0, &amp;__main_block_desc_0_DATA));
</code></pre><p><strong>main_block_func_0&#x7ED3;&#x6784;&#x4F53;&#x58F0;&#x660E;&#xFF0C;</strong>main_block_func_0&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;<strong>cself&#x662F; </strong>main_block_impl_0 &#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#xFF0C;&#x8BE5;&#x5B9E;&#x4F8B;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x8F6C;&#x6362;&#x524D;&#x7684;Block&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code>static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

            printf(&quot;Block\n&quot;);
        }
</code></pre><p>&#x53BB;&#x6389;&#x8F6C;&#x6362;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code>struct __main_block_impl_0 temp = __main_block_impl_0(__main_block_func_0,&amp;__main_block_desc_0_DATA);
struct __main_block_impl_0 *blk = &amp;temp;
</code></pre><p>&#x4E0A;&#x8FB9;&#x4EE3;&#x7801;&#x5BF9;&#x5E94;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>void (^blk)(void) = ^{
            printf(&quot;Block\n&quot;);
        };
</code></pre><p>&#x518D;&#x6765;&#x770B;&#x770B;Block&#x8C03;&#x7528;&#x90E8;&#x5206;&#xFF1A;blk()</p>
<pre><code>((void (*)(__block_impl *))((__block_impl *)blk)-&gt;FuncPtr)((__block_impl *)blk);
</code></pre><p>&#x53BB;&#x6389;&#x8F6C;&#x6362;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code>(*blk-&gt;impl.FuncPtr)(blk);
</code></pre><blockquote>
<p>&#x6B63;&#x5982;&#x521A;&#x624D;&#x770B;&#x5230;&#x7684;&#x7F16;&#x8BD1;&#x540E;&#x7684;&#x4EE3;&#x7801;&#xFF0C;block&#x8BED;&#x6CD5;&#x8F6C;&#x6362;&#x7684;<strong>main_block_func_0&#x51FD;&#x6570;&#x7684;&#x6307;&#x9488;&#x88AB;&#x8D4B;&#x503C;&#x6210;&#x5458;&#x53D8;&#x91CF;FuncPtr&#x4E2D;&#xFF0C;&#x53E6;&#x5916;&#x4E5F;&#x8BF4;&#x660E;&#x4E86;</strong>main_block_func_0&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;__cself&#x6307;&#x5411;Block&#x503C;&#x3002;&#x5728;&#x8C03;&#x7528;&#x8BE5;&#x4EE3;&#x7801;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x53EF;&#x4EE5;&#x770B;&#x51FA;Block&#x6B63;&#x662F;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4E86;&#x4F20;&#x9012;&#x3002;</p>
</blockquote>
<h5 id="block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A;-&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;c&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;mainblockimpl0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;"><a name="block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A;-&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;c&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;mainblockimpl0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;" class="anchor-navigation-ex-anchor" href="#block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A;-&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;c&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;mainblockimpl0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A;-&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;c&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;mainblockimpl0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;" class="plugin-anchor" href="#block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A;-&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;c&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;mainblockimpl0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;"><i class="fa fa-link" aria-hidden="true"></i></a>Block&#x5B9E;&#x8D28;&#x5C31;&#x662F;&#xFF1A; &#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x628A;Block&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x53CA;Block&#x53D8;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x822C;&#x7684;C&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F53;&#x3001;&#x51FD;&#x6570;&#xFF0C;block&#x8C03;&#x7528;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x6307;&#x9488;&#x8C03;&#x7528;&#x3002;Block&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF08;__main_block_impl_0&#xFF09;&#x4F1A;&#x6301;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8BE5;&#x7ED3;&#x6784;&#x4F53;&#x4F1A;&#x5E26;&#x7740;&#x9ED8;&#x8BA4;&#x503C;&#x521D;&#x59CB;&#x5316;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x4F8B;&#x3002;</h5>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../neicun/4.5.html" class="navigation navigation-prev " aria-label="Previous page: 4.5内存管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="5.1.html" class="navigation navigation-next " aria-label="Next page: 5.1Block本质">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5、Block","level":"1.6","depth":1,"next":{"title":"5.1Block本质","level":"1.6.1","depth":2,"path":"md/block/5.1.md","ref":"md/block/5.1.md","articles":[]},"previous":{"title":"4.5内存管理","level":"1.5.5","depth":2,"path":"md/neicun/4.5.md","ref":"md/neicun/4.5.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","donate","sharing-plus","anchor-navigation-ex","favicon","livereload"],"styles":{"website":"./styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"github":{"url":"https://github.com/zhou415770319"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"../../img/alipay.JPG","alipayText":" ","button":"赞赏","title":"","wechat":"","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"zhou415770319","repo":"iosKnowledges","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","author":"zhoufei","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"zhoufei-iosknowledges","language":"zh-hans","links":{"sidebar":{"我的博客":"http://www.feiyum.cn/index/"}},"gitbook":"3.2.3","description":"ios面试知识点总结"},"file":{"path":"md/block/5.md","mtime":"2019-05-03T14:40:51.305Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-03T14:51:39.741Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

